package com.ikkong.school.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.ikkong.common.vo.User;
import com.ikkong.core.base.BaseController;
import com.ikkong.core.dao.Blade;
import com.ikkong.core.jfinal.ext.kit.JsonKit;
import com.ikkong.core.jfinal.ext.shiro.ShiroKit;
import com.ikkong.core.toolbox.Record;
import com.ikkong.school.model.Product;
import com.ikkong.school.model.Suppliers;
import com.ikkong.system.controller.base.UrlPermissController;
import com.ikkong.school.model.Putin;
import com.ikkong.school.service.PutinService;
import com.ikkong.school.service.impl.PutinServiceImpl;

import javax.swing.plaf.basic.BasicLabelUI;

/**
 * Generated by Blade.
 * 2018-07-12 23:50:39
 */
public class PutinController extends UrlPermissController {
	private static String CODE = "putin";
	private static String PERFIX = "putin";
	private static String LIST_SOURCE = "Putin.list";
	private static String BASE_PATH = "/platform/putin/";
	
	PutinService service = new PutinServiceImpl();
	
	public void index() {
		setAttr("code", CODE);
		render(BASE_PATH + "putin.html");
	}

	public void pass() {
        Integer pass = getParaToInt("pass",1);
        Integer ids = getParaToInt("id", 0);
        Suppliers byId = Blade.create(Suppliers.class).findById(ids);
        if(byId == null){
            renderJson(error("未找到该供应商"));
            return;
        }
        Integer u_id = byId.getU_id();
        User user = Blade.create(User.class).findById(u_id);
        if(user == null){
            renderJson(error("未找到该供应商"));
            return;
        }

        user.setStatus(pass);
        boolean update = Blade.create(User.class).update(user);
        if (update) {
            renderJson(success("审核成功"));
        } else {
            renderJson(error("审核失败"));
        }
	}

	public void add() {
		setAttr("code", CODE);
		render(BASE_PATH + "putin_add.html");
	}

	public void edit() {
		String id = getPara(0);
		Putin putin = service.findById(id);
		setAttr("model", JsonKit.toJson(putin));
		setAttr("id", id);
		setAttr("code", CODE);
		render(BASE_PATH + "putin_edit.html");
	}

	public void view() {
		String id = getPara(0);
		Putin putin = service.findById(id);
		setAttr("model", JsonKit.toJson(putin));
		setAttr("id", id);
		setAttr("code", CODE);
		render(BASE_PATH + "putin_view.html");
	}

	public void list() {
        Object grid = paginate(LIST_SOURCE);
		renderJson(grid);
	}

	public void save() {
        String code = getPara("code");
        List<Map> codelist = Blade.dao().select("Putin.bycode", Map.class, Record.create().set("code", code).set("school", ShiroKit.getUser().getDeptId().toString()));
        String bid = "";
        Date cgdate = new Date();
        Date buydate = new Date();
        int pday = 0;
        int school = 0;
        boolean save = false;
        for(Map map :codelist){
            school = (int)(map.get("school")==null?school:map.get("school"));
            pday = (int)(map.get("pday")==null?pday:map.get("pday"));
            bid = (map.get("bid")==null?bid:map.get("bid")).toString();
            cgdate=(Date)(map.get("cgdate")==null?cgdate:map.get("cgdate"));
            buydate=(Date)(map.get("buydate")==null?buydate:map.get("buydate"));
            int pid=(int)(map.get("pid")==null?0:map.get("pid"));
            Putin putin = new Putin();
            putin.setP_id(pid);
            putin.setB_code(bid);
            putin.setLife(pday);
            putin.setSchool_id(school);
            putin.setIn_date(buydate);
            putin.setMade_date(cgdate);
            save = service.save(putin);
        }

		if (save) {
			renderJson(success(SAVE_SUCCESS_MSG));
		} else {
			renderJson(error(SAVE_FAIL_MSG));
		}
	}

	public void update() {
		Putin putin = mapping(PERFIX, Putin.class);
		boolean temp = service.update(putin);
		if (temp) {
			renderJson(success(UPDATE_SUCCESS_MSG));
		} else {
			renderJson(error(UPDATE_FAIL_MSG));
		}
	}

	public void remove() {
		String ids = getPara("ids");
		int cnt = service.deleteByIds(ids);
		if (cnt > 0) {
			renderJson(success(DEL_SUCCESS_MSG));
		} else {
			renderJson(error(DEL_FAIL_MSG));
		}
	}
}
